using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.IO;

namespace Pixysoft.Security
{
    public class UnicodeEscapeSequence
    {
        private UnicodeEscapeSequence()
        {
        }

        /// <summary>
        /// unicodeתstring \u5927
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Decript(string data)
        {
            lock (typeof(UnicodeEscapeSequence))
            {
                char c_data = char.MinValue;

                try
                {
                    if (data == null || data.Trim() == "")
                        return data;

                    ByteBuilder builder = new ByteBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        c_data = data[i];

                        if (c_data == '=')
                        {
                            char[] hex_data = new char[2];

                            if (data[i + 1] == '\r')
                            {
                                i++;
                                i++;
                                continue;
                            }


                            i++;
                            hex_data[0] = data[i];

                            i++;
                            hex_data[1] = data[i];

                            int val = int.Parse(hex_data[0].ToString() + hex_data[1].ToString(), System.Globalization.NumberStyles.HexNumber);

                            builder.Append((byte)val);
                        }
                        else
                        {
                            builder.Append((byte)(int)c_data);
                        }

                    }

                    return Pixysoft.StringCoder.ByteToString(builder.ToByte());
                }
                catch
                {
                    return data;
                }
            }
        }

        public static byte[] XDecript(string data)
        {
            lock (typeof(UnicodeEscapeSequence))
            {
                char c_data = char.MinValue;

                try
                {
                    if (data == null || data.Trim() == "")
                        return Pixysoft.StringCoder.StringToByte(data);

                    ByteBuilder builder = new ByteBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        c_data = data[i];

                        if (c_data == '=')
                        {
                            char[] hex_data = new char[2];

                            if (data[i + 1] == '\r')
                            {
                                i++;
                                i++;
                                continue;
                            }


                            i++;
                            hex_data[0] = data[i];

                            i++;
                            hex_data[1] = data[i];

                            int val = int.Parse(hex_data[0].ToString() + hex_data[1].ToString(), System.Globalization.NumberStyles.HexNumber);

                            builder.Append((byte)val);
                        }
                        else
                        {
                            builder.Append((byte)(int)c_data);
                        }

                    }

                    return builder.ToByte();
                }
                catch
                {
                    return Pixysoft.StringCoder.StringToByte(data);
                }
            }
        }

        private void test()
        {
            //9.4.1 Unicode escape sequences

            //http://tc.wangchao.net.cn/bbs/detail_74922.html

            Console.WriteLine(Decript("\u0066\u57ce\u5c0f\u683c\u6d4b\u8bd5\u4fe1\u606f"));

            string value = Decript("\u0066\u57ce\u5c0f\u683c\u6d4b\u8bd5\u4fe1\u606f");

            Console.WriteLine(Decript("\u4041\u4243\u4040\u4040\u4040\u4040\u4040\u4040"));
        }
    }
}
